package com.earl.gsm.dao.impl.security;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.earl.gsm.commons.dao.impl.BaseDaoImpl;
import com.earl.gsm.dao.PrivilegeDao;
import com.earl.gsm.dao.RoleDao;
import com.earl.gsm.dao.RolePrivilegeDao;
import com.earl.gsm.domain.sercurity.privilege.Privilege;
import com.earl.gsm.domain.sercurity.role.Role;
import com.earl.gsm.domain.sercurity.roleprivilege.RolePrivilege;

@Repository("roleDao")
public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao{
   
	@Resource
	RolePrivilegeDao rolePrivilegeDao;
	
	@Resource
	PrivilegeDao privilegeDao;
	
	/**
	 * 删除角色的时候，级联删除角色权限关系.
	 * @author 黄祥谦.
	 */
	@Override
	public void deleteCascade(Long roleId){
		this.delete(roleId);
		RolePrivilege rolePrivilege = new RolePrivilege();
		rolePrivilege.setRoleId(roleId);
		rolePrivilegeDao.deleteByQuery(rolePrivilege);
	}

	@Override
	public List<Privilege> findRolePrivilege(Long roleId) {
		// TODO Auto-generated method stub
		
		RolePrivilege rp = new RolePrivilege();
		rp.setRoleId(roleId);
		List<RolePrivilege> rpList = rolePrivilegeDao.searchQuery(rp);
		List<Long> tmp = new ArrayList<Long>();
		
		for (RolePrivilege long1 : rpList) {
			tmp.add(long1.getPrivilegeId());
		}
		
		return privilegeDao.findByList(tmp);
	}
}